Skip to main content

Workflow Loop Template

The Workflow Loop template demonstrates how a workflow can repeat a sequence of nodes until a specified condition becomes false. Instead of duplicating workflow steps, a Loop node repeatedly executes the same workflow branch while updating the workflow context after each iteration. This template introduces iterative execution into BindAI workflows.

Purpose

This template demonstrates how to:
  • repeat workflow execution
  • evaluate a loop predicate
  • update workflow variables
  • exit the loop safely
  • build iterative automation
It extends the Condition template by allowing execution to repeat instead of choosing only one path.

Workflow Structure

A loop creates a cycle in the workflow.
Execution continues until the loop condition evaluates to False.

Execution Flow

Each iteration follows the same sequence.
The workflow exits only when the predicate returns False.

Loop Predicate

The Loop node evaluates a predicate before each iteration. Conceptually:
If the predicate returns:
  • True → execute another iteration
  • False → continue to the workflow exit

Workflow Variables

Loops usually depend on workflow variables. Example:
Each iteration updates the shared workflow context.

Example

Suppose an AI agent processes one item per iteration.
The workflow continues until every item has been processed.

Shared Context

Every iteration shares the same workflow context.
Variables accumulate across iterations unless explicitly reset.

Loop Exit

Eventually the predicate evaluates to false.
Execution leaves the loop and proceeds normally.

Polling Example

Loops are useful when waiting for an external process.
Once the external operation finishes, the workflow exits the loop.

Batch Processing

A common use case is processing collections.
This avoids creating separate workflow branches for every item.

Loop vs Retry

Although they both involve repeated execution, loops and retries solve different problems. Retries recover from temporary failures. Loops intentionally repeat workflow logic.

Preventing Infinite Loops

Every loop should eventually terminate. A good pattern is:
Without updating the state used by the predicate, the workflow may never exit.

Related Templates

This template prepares you for:
  • Workflow Parallel
  • Workflow Human
  • Workflow Retry
  • Workflow Timeout
Many production workflows combine loops with these patterns.

Best Practices

  • Always define a clear exit condition.
  • Update workflow variables during each iteration.
  • Keep loop bodies focused on one responsibility.
  • Avoid deeply nested loops.
  • Use retries instead of loops for transient failures.
  • Monitor long-running loops in production.

Summary

The Workflow Loop template demonstrates how BindAI workflows can repeat execution until a predicate becomes false. By combining shared workflow context with iterative execution, loops provide a clean and reusable way to process collections, poll external systems, and implement business logic without duplicating workflow nodes.